<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div class="layui-form">





</div>

<table class="layui-hide" id="test" lay-filter="test"></table>


<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="/js/jquery-1.4.4.min.js"></script>
<script src="/js/layui/layui.all.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['table','layer'], function(){
        var table = layui.table;
        var layer = layui.layer;
        table.render({
            elem: '#test'
            ,url:'/getProductByPage'
            ,cols: [[
                {field:'product_id', title:'ID', fixed: 'left', unresize: true, sort: true}
                ,{field:'product_name', title:'商品名', edit: 'text'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]],
            response:{
                statusCode:"200"
            }
            ,page: true
        });

        table.render('test',{ //其它参数在此省略
            url: '/getProductByPage',
            where: {username: $("#username").val()}
        });


        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.prompt({
                    formType: 2
                    ,value: data.email
                }, function(value, index){
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });


       $("#selBtn").click(function () {
           table.render('#test',{ //其它参数在此省略
               url: '/getProductByArrayPage',
               where: {username: $("#username").val()}
           });
       })

        $("#addBtn").click(function () {
           layer.open({
                   type:2,
                   content:"/pages/addUser.html",
                    aera:["800px","600px"]
           }
           )
        })


    });

</script>

</body>
</html>